/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 6/22/18
Time: 4:11 PM
 */

package Cn.Sarkar.MMS.LayerOne

import Cn.Sarkar.Entities.Common.Additional.IDescription
import Cn.Sarkar.Entities.Common.Additional.IName
import Cn.Sarkar.Entities.MMS.Additional.IContacts
import Cn.Sarkar.Entities.MMS.Additional.ICountry
import Cn.Sarkar.Entities.MMS.Independent.ICompany
import Cn.Sarkar.Entities.MMS.ToJsonEntities.JCompany
import Cn.Sarkar.Entities.MMS.ToJsonEntities.JContacts
import Cn.Sarkar.Entities.MMS.ToJsonEntities.JCountry
import Cn.Sarkar.Entities.ToJsonEntities.JDescription
import Cn.Sarkar.Entities.ToJsonEntities.JName
import com.google.gson.*
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import org.joda.time.DateTime
import java.lang.reflect.Type

fun GsonBuilder.registerMMSInterfaceInstances() {
    registerTypeAdapter(DateTime::class.java, object : TypeAdapter<DateTime>() {
        //        val pattern = "yyyy-MM-dd HH:mm:ss"
        override fun write(out: JsonWriter, value: DateTime) {
            out.value(value.toString())
        }

        override fun read(read: JsonReader): DateTime {
//            return DateTime(SimpleDateFormat().parse(read.nextString()).time)
            return DateTime.parse(read.nextString())
        }

    })

    registerTypeAdapter(IName::class.java, object : JsonDeserializer<IName>, JsonSerializer<IName> {
        override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): IName {
            return context.deserialize(json, JName::class.java) as JName
        }

        override fun serialize(src: IName, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
            return context.serialize(src)
        }
    })

    registerTypeAdapter(IDescription::class.java, object : JsonDeserializer<IDescription>, JsonSerializer<IDescription> {
        override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): IDescription {
            return context.deserialize(json, JDescription::class.java) as JDescription
        }

        override fun serialize(src: IDescription, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
            return context.serialize(src)
        }
    })

    registerTypeAdapter(ICompany::class.java, object : JsonDeserializer<ICompany>, JsonSerializer<ICompany> {
        override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): ICompany {
            return context.deserialize(json, JCompany::class.java) as ICompany
        }

        override fun serialize(src: ICompany, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
            return context.serialize(src)
        }
    })

    registerTypeAdapter(IContacts::class.java, object : JsonDeserializer<IContacts>, JsonSerializer<IContacts> {
        override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): IContacts {
            return context.deserialize(json, JContacts::class.java) as IContacts
        }

        override fun serialize(src: IContacts, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
            return context.serialize(src)
        }
    })

    registerTypeAdapter(ICountry::class.java, object : JsonDeserializer<ICountry>, JsonSerializer<ICountry> {
        override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): ICountry {
            return context.deserialize(json, JCountry::class.java) as ICountry
        }

        override fun serialize(src: ICountry, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
            return context.serialize(src)
        }
    })
}